package cn.lystudio.rohmtimeopwide_background_v11.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.RecordDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohmtimeopwide_background_v11.exceptions.JsonException;
import cn.lystudio.rohmtimeopwide_background_v11.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/record")
@Slf4j
public class RecordController {
    private RecordService recordService;
    @Lazy
    @Autowired
    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }

    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page,
                                        Integer limit,
                @RequestParam(value = "userCode",defaultValue = "",required = false)String userCode,
                @RequestParam(value = "machineNo",defaultValue = "",required = false)String machineNo,
                @RequestParam(value = "recordType",defaultValue = "",required = false)String recordTypeStr,
                @RequestParam(value = "searchDateRange",defaultValue = "",required = false)String searchDateRangeStr,
                @RequestParam(value="LotNo",defaultValue = "",required = false)String LotNo,
                @RequestParam(value="AQRNo",defaultValue = "",required = false)String AQRNo,
                @RequestParam(value="otherKeywords",defaultValue = "")String otherKeywords){
        // 1.整理参数
        List<String> recordTypeList=null;
        String searchStartTime= StrUtil.EMPTY;
        String searchEndTime=StrUtil.EMPTY;

        if(StrUtil.isNotEmpty(recordTypeStr)){
            recordTypeList = (List<String>) Convert.toList(recordTypeStr.split(";|；"));
        }
        if(StrUtil.isNotEmpty(searchDateRangeStr)){
            String[] tempArr = searchDateRangeStr.split("~|~");
            if(tempArr.length>=2){
                searchStartTime = DateUtil.format(DateUtil.parse(tempArr[0].trim()+" 00:00:00"),"yyyy-MM-dd HH:mm:ss");
                searchEndTime = DateUtil.format(DateUtil.parse(tempArr[1].trim()+" 00:00:00"),"yyyy-MM-dd HH:mm:ss");
            }
        }

        // 2.查询数据
        PageUtil.setFirstPageNo(1);
        List<RecordDTO> all = recordService.getAll(userCode, machineNo, recordTypeList, searchStartTime, searchEndTime, LotNo, AQRNo, otherKeywords);
        if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
    }

    @PostMapping("/delRecord")
    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    public SaResult delRecord(Integer recordID){
        return SaResult.data(recordService.delRecord(recordID));
    }

    @GetMapping("/getDailyData4Console")
    @SaIgnore
    public LayuiPageApiResponse getDailyData4Console(@RequestParam(value = "machineNo",defaultValue = "") String machineNo,
                                                     @RequestParam(value = "dailyStart",defaultValue = "") String dailyStart,
                                                     @RequestParam(value = "dailyEnd",defaultValue = "") String dailyEnd){
        if(StrUtil.isEmpty(machineNo) || dailyStart.equals("0") || dailyEnd.equals("0")){
            throw new JsonException(500,"请求数据异常");
        }
        return LayuiPageApiResponse.ofSuccessData(recordService.getDailyData4Console(machineNo, dailyStart,dailyEnd),0);
    }

}
